﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using prototype123.data_access;
using prototype123.controllers;
using prototype123.models;

namespace prototype123
{
    public partial class main_form : Form
    {

        ArrayList students;
        ArrayList theCourses;
        
        ArrayList theBooks;
        ArrayList current_student_courses;
        ArrayList categories;

        // Initializes the main_form. Initially, it is invisible
        public main_form()
        {
            InitializeComponent();
            this.Visible = false;
        }

        // Ensures that the program closes when the main_form closes
        private void main_form_FormClosing(object sender, FormClosingEventArgs e)
        {
            prototype123.Program.l_frm.Close();
        }

        // Shows the End Term form
        private void endTermToolStripMenuItem_Click(object sender, EventArgs e)
        {
            end_term frm = new end_term();
            frm.Show();
        }

        //exit the application
        private void menuQuit_Click(object sender, EventArgs e)
        {
            prototype123.Program.l_frm.Close();
        }

        //add new assignment to student assignment list
        private void btnAddAssignment_Click(object sender, EventArgs e)
        {
            //verify that name, points earned, points possible, and category are all filled in

            //maintainAssignments.AddAssignment(txtName, txtPtsEarned, txtPtsPossible, txtCat)
        }

        //edits an assignment that is currently in the student assign list
        private void btnUpdateAssignment_Click(object sender, EventArgs e)
        {
            //pre-requires: user has selected an assignment and (most likely) changed some of the text boxes

            //verify that name, points earned, points possible, and category are all filled in

            //maintainAssignments.UpdateAssignment(txtName, txtPtsEarned, txtPtsPossible, txtCat)
        }

        //deletes an assignment that is currently in the student assign list
        private void btnDeleteAssignment_Click(object sender, EventArgs e)
        {
            //pre-requires: the user has selected an assignment from the datagrid assignment list

            //maintainAssignments.DeleteAssignment(assignmentID)
        }



        //adds a TermItem to the database and to the Form.
        private void btnAddCourse_Click(object sender, EventArgs e)
        {
            //verify that the course name, teacher type, and textbook have all been entered
            
           if (cmbCourseName.Text == "" || cmbTextbook.Text == "" || cmbCourseTeacher.Text == "")
            {
                MessageBox.Show("Please fill out the course name, teacher type, and textbook in order to add a course.");
                return;
            }

            Course crse = (Course)theCourses[cmbCourseName.SelectedIndex];
            Textbook book = (Textbook)theBooks[cmbTextbook.SelectedIndex];
            
            MaintainCourseRegController mcg = new MaintainCourseRegController();


            //SystemController.Instance.CurrentStudent.StudentID
            TermItem newTI = mcg.AddCourseReg(SystemController.Instance.CurrentStudent.StudentID,
               crse.CourseID , book.Title, cmbCourseTeacher.Text[0].ToString(), false);

            
            if (newTI == null)
            {
                MessageBox.Show("Student cannot add that course currently, please select another course");
                return;
            }

            CourseDAO tidao = new CourseDAO(Connector.Instance.Connection);

            Course newcourse = tidao.Load(newTI.CourseID);

            current_student_courses.Add(newcourse);
            listCourses.Items.Add(newcourse.Title);
            cmbCourseSelect.Items.Add(newcourse.Title);

        }


        // Activates when the user selects a new student from the combobox
        // fills in the courses for the new student into the listCourses and the comboBox of courses
        private void cmbStudents_SelectedIndexChanged(object sender, EventArgs e)
        {

            string name = cmbStudents.SelectedItem.ToString();
            Student stud = (Student)students[cmbStudents.SelectedIndex];
            SystemController.Instance.CurrentStudent = stud;

            current_student_courses = new ArrayList();

            categories = new ArrayList();
            listCategories.Items.Clear();
            
            listCourses.Items.Clear();
            cmbCourseSelect.Items.Clear();

            toolStripCurrentStudent.Text = "Current Student: " + stud.FirstName + " " + stud.LastName;

            CourseDAO couDAO = new CourseDAO(Connector.Instance.Connection);
            StudentTermDAO stdao = new StudentTermDAO(Connector.Instance.Connection);
            int current_term = stdao.GetCurrentStudentTerm(stud.StudentID);

            ArrayList courses = couDAO.Find(stud.StudentID, current_term);

            foreach (Course course in courses)
            {
                current_student_courses.Add(course);
                TermItemDAO tidao = new TermItemDAO(Connector.Instance.Connection);
                TermItem tm = tidao.Load(stud.StudentID, stdao.GetCurrentStudentTerm(stud.StudentID), course.CourseID);
                // if it is dropped, then load it in with a (d) next to it
                if (tm.Dropped == 1)
                {
                    listCourses.Items.Add(course.Title + "    (d)");
                    cmbCourseSelect.Items.Add(course.Title + "    (d)");
                }
                else // if it is not dropped, then don't load it in with a (d) next to it
                {
                    listCourses.Items.Add(course.Title);
                    cmbCourseSelect.Items.Add(course.Title);
                }

            }
        }


        // Fills in the User's students into a combobox
        // Fills in the textbooks into a combobox
        // Fills in the courses into the Hei course list
        private void main_form_Load(object sender, EventArgs e)
        {

            StudentDAO studao = new StudentDAO(Connector.Instance.Connection);
            students = studao.Load(SystemController.Instance.CurrentEducator.EducatorID);

            foreach (Student stud in students)
            {
                cmbStudents.Items.Add(stud.FirstName + " " + stud.LastName);
            }


            CourseDAO cdao = new CourseDAO(Connector.Instance.Connection);
            theCourses = cdao.LoadAllCourses();
            foreach (Course course in theCourses)
            {
                cmbCourseName.Items.Add(course.Title);
            }

            TextbookDAO tdao = new TextbookDAO(Connector.Instance.Connection);
            theBooks = tdao.LoadAllTextbooks();
            foreach(Textbook book in theBooks)
            {

                cmbTextbook.Items.Add(book.Title);
            }

        }

        // Calls the Controller reponsible for dropping a class from the database.
        // The dropped course displays a "(d)" next to it in the combobox and listbox.
        private void btnDropCourse_Click(object sender, EventArgs e)
        {

            MaintainCourseRegController mcg = new MaintainCourseRegController();

            Course droppedCourse = (Course)current_student_courses[listCourses.SelectedIndex];

            mcg.DropCourse(SystemController.Instance.CurrentStudent.StudentID, droppedCourse.CourseID);

            listCourses.Items[listCourses.SelectedIndex] = droppedCourse.Title  + "    (d)";
            cmbCourseSelect.Items[listCourses.SelectedIndex] = droppedCourse.Title + "    (d)";

        
        }

        // Clears the dropped flag in the database for a course
        // Takes away the (d) text from the text in the combobox and listbox
        private void btnReAddCourse_Click(object sender, EventArgs e)
        {
            MaintainCourseRegController mcg = new MaintainCourseRegController();

            Course readdedCourse = (Course)current_student_courses[listCourses.SelectedIndex];

            mcg.ReAddCourse(SystemController.Instance.CurrentStudent.StudentID, readdedCourse.CourseID);

            listCourses.Items[listCourses.SelectedIndex] = readdedCourse.Title;
            cmbCourseSelect.Items[listCourses.SelectedIndex] = readdedCourse.Title;
        }

        // Activates when the user selects a different course on the Assignments Page
        // Puts the appropriate Categories in the cmbAssignCategory combobox
        private void cmbCourseSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblCourseName.Text = "Assignments for " + cmbCourseSelect.Items[cmbCourseSelect.SelectedIndex];

            cmbAssignCategory.Items.Clear();
            Course current_course = (Course)current_student_courses[cmbCourseSelect.SelectedIndex];
            Student stud = SystemController.Instance.CurrentStudent;

            StudentTermDAO stdao = new StudentTermDAO(Connector.Instance.Connection);
            TermItemDAO tidao = new TermItemDAO(Connector.Instance.Connection);


            TermItem tm = tidao.Load(stud.StudentID, stdao.GetCurrentStudentTerm(stud.StudentID), current_course.CourseID);



            AssignmentCategoryDAO acDAO = new AssignmentCategoryDAO(Connector.Instance.Connection);
            ArrayList items = acDAO.Find(tm.ID);

            categories = items;


            foreach (AssignmentCategory cat in categories)
            {
                cmbAssignCategory.Items.Add(cat.CategoryName + "  " + cat.CategoryPercent + "%");
            }
        }




        /*
        private void listCourses_SelectedIndexChanged(object sender, EventArgs e)
        {

            listCategories.Items.Clear();
            Course current_course = (Course)current_student_courses[listCourses.SelectedIndex];
            Student stud = prototype123.Program.sys_ctrler.CurrentStudent;

            StudentTermDAO stdao = new StudentTermDAO(Connector.Instance.Connection);
            TermItemDAO tidao = new TermItemDAO(Connector.Instance.Connection);


            TermItem tm = tidao.Load(stud.StudentID, stdao.GetCurrentStudentTerm(stud.StudentID), current_course.CourseID);



            AssignmentCategoryDAO acDAO = new AssignmentCategoryDAO(Connector.Instance.Connection);
            ArrayList items = acDAO.Find(tm.ID);

            categories = items;


            foreach (AssignmentCategory cat in categories)
            {
                listCategories.Items.Add(cat.CategoryName + "  " + cat.CategoryPercent + "%");
            }

            
        }

        */

        //adds a new category to the database


        /*
        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            //verify that category name and category weight have been filled in

            //maintainCategory.AddCategory(txtCatName, txtCatWeight)

            Student stud = Program.sys_ctrler.CurrentStudent;

            Course current_course = (Course)current_student_courses[listCourses.SelectedIndex];

            StudentTermDAO stdao = new StudentTermDAO(Connector.Instance.Connection);

            TermItemDAO tiDAO = new TermItemDAO(Connector.Instance.Connection);

            AssignmentCategoryDAO astcdao = new AssignmentCategoryDAO(Connector.Instance.Connection);

            TermItem tm = tiDAO.Load(stud.StudentID, stdao.GetCurrentStudentTerm(stud.StudentID), current_course.CourseID);

            AssignmentCategory cat = new AssignmentCategory(txtCategoryName.Text, Convert.ToInt32(txtCategoryWeight.Text));

            astcdao.AddCategory(cat, tm.ID);

            categories.Add(cat);

            listCategories.Items.Add(cat.CategoryName + "  " + cat.CategoryPercent + "%");


        }
         * 
         */

        /*
        //deletes a category from the database
        private void btnDeleteCategory_Click(object sender, EventArgs e)
        {
            //verify that a category has been selected from listCategories

            //maintainCategory.DeleteCategory(CatID)
        }
        */


    }
}
